<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:replace="admin/_fragments::head(~{::title})">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>博客管理</title>

    <link href="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.css" rel="stylesheet">
    <link rel="stylesheet" href="../../static/css/main.css">
</head>
<body>
<!--顶部导航-->
<header th:replace="admin/_fragments::header(1)" class="ui inverted attached segment m-shadow-small">
    <div class="ui container">
        <nav class="ui inverted secondary stackable menu">
            <h2 class="ui header teal item m-header-logo">管理后台</h2>
            <a href="#" class="active m-header-item m-hide-on-mobile item"><i class="small home icon"></i> 博客</a>
            <a href="#" class="m-header-item m-hide-on-mobile item"><i class="small idea icon"></i> 分类</a>
            <a href="#" class="m-header-item m-hide-on-mobile item"><i class="small tag icon"></i> 标签</a>
            <div class="right menu m-header-item m-hide-on-mobile">
                <div class="ui dropdown item">
                    <div class="text">
                        <img src="https://picsum.photos/50" alt="" class="ui avatar image"> Misty
                    </div>
                    <i class="dropdown icon"></i>
                    <div class="menu">
                        <a href="#" class="item">注销</a>
                    </div>
                </div>
            </div>
        </nav>
    </div>

    <a href="#" class="ui black icon button m-header-toggle-icon m-show-on-mobile toggle">
        <i class="sidebar icon"></i>
    </a>
</header>

<!--二级导航-->
<div class="ui attached pointing menu">
    <div class="ui container">
        <div class="right menu">
            <a href="#" class="item" th:href="@{/admin/blogs/input}">发布</a>
            <a href="#" class="active item" th:href="@{/admin/blogs}">列表</a>
        </div>
    </div>
</div>

<!--中间内容-->
<section class="m-article-container m-padding-tb-large">
    <div class="ui container">
        <div class="ui secondary segment form">
            <input type="hidden" name="page">
            <div class="inline fields">
                <div class="field">
                    <input type="text" name="title" placeholder="标题">
                </div>
                <div class="field">
                    <div class="ui selection dropdown">
                        <input type="hidden" name="categoryId">
                        <i class="dropdown icon"></i>
                        <div class="default text">分类</div>
                        <div class="menu">
                            <div class="item" data-value="">全部</div>
                            <div class="item" data-value="1" th:each="cate : ${categories}"
                                 th:attr="data-value=${cate.id}" th:text="${cate.name}">
                                错误日志
                            </div>
                            <!--/*-->
                            <div class="item" data-value="2">开发者手册</div>
                            <!--*/-->
                        </div>
                    </div>
                </div>
                <div class="field">
                    <div class="ui checkbox">
                        <input type="checkbox" id="recommend" name="recommend">
                        <label for="recommend">推荐</label>
                    </div>
                </div>
                <div class="field">
                    <button type="button" id="searchBtn" onclick="search()" class="ui mini teal basic button"><i
                            class="search icon"></i>搜索
                    </button>
                </div>
            </div>
        </div>

        <div id="table-container">
            <table th:fragment="blogList" class="ui compact teal table">
                <thead>
                <tr>
                    <th></th>
                    <th>标题</th>
                    <th>分类</th>
                    <th>推荐</th>
                    <th>状态</th>
                    <th>最后更新</th>
                    <th>操作</th>
                </tr>
                </thead>
                <tbody>
                <!--/*@thymesVar id="article" type="org.misty.blog.po.Article"*/-->
                <tr th:each="article,iterStat : ${page.content}" th:object="${article}">
                    <td th:text="${iterStat.count}">1</td>
                    <td th:text="*{title}">刻意练习清单</td>
                    <td th:text="*{category.name}">认知升级</td>
                    <td th:text="*{recommend} ? '是' : '否'">是</td>
                    <td th:text="*{published} ? '发布' : '草稿'">发布</td>
                    <td th:text="*{#dates.format(updateTime, 'yyyy-MM-dd HH:mm')}">2017-10-02 09:45</td>
                    <td>
                        <a href="#" class="ui mini teal basic button"
                           th:href="@{/admin/blogs/{id}/input(id=*{id})}">编辑</a>
                        <a href="#" class="ui mini red basic button"
                           th:href="@{/admin/blogs/{id}/delete(id=*{id})}">删除</a>
                    </td>
                </tr>
                </tbody>
                <tfoot>
                <tr>
                    <th colspan="7">
                        <div class="ui mini pagination menu" th:if="${page.totalPages > 1}">
                            <a class="item" th:if="${page.hasPrevious()}"
                               onclick="page(this)" th:attr="data-page=${page.number}-1">上一页</a>

                            <a class="item" th:if="${page.hasNext()}"
                               onclick="page(this)" th:attr="data-page=${page.number}+1">下一页</a>
                        </div>
                        <a href="" class="ui mini right floated teal basic button"
                           th:href="@{/admin/blogs/input}">新增</a>
                    </th>
                </tr>
                </tfoot>
            </table>
        </div>
    </div>
</section>

<!--footer-->
<footer th:replace="admin/_fragments::footer" class="ui vertical inverted segment m-footer">
    <div class="ui container center aligned">
        <div class="ui stackable grid divided inverted">
            <!--二维码-->
            <div class="three wide column">
                <div class="ui inverted link list">
                    <div class="item">
                        <img src="https://via.placeholder.com/110?text=WeChat" class="ui rounded image" alt="">
                    </div>
                </div>
            </div>
            <!--二维码-->

            <!--最新博客-->
            <div class="three wide column">
                <h4 class="ui inverted header m-text-thin m-text-spacing">最新博客</h4>
                <div class="ui inverted link list">
                    <a href="#" class="item">用户故事 (User Story)</a>
                    <a href="#" class="item">用户故事 (User Story)</a>
                    <a href="#" class="item">用户故事 (User Story)</a>
                </div>
            </div>
            <!--最新博客-->

            <!--联系我-->
            <div class="three wide column">
                <h4 class="ui inverted header m-text-thin m-text-spacing">联系我</h4>
                <div class="ui inverted link list">
                    <a href="#" class="item">Email:</a>
                    <a href="#" class="item">QQ:</a>
                </div>
            </div>
            <!--联系我-->

            <!--关于-->
            <div class="seven wide column">
                <h4 class="ui inverted header m-text-thin m-text-spacing">关于</h4>
                <p class="m-text-thin m-text-spacing m-opacity-80">這是我的個人博客</p>
            </div>
            <!--关于-->

        </div>
        <div class="ui inverted section divider"></div>
        <p class="m-text-thin m-text-spacing m-opacity-60">by Misty</p>
    </div>
</footer>

<!--/*/ <th:block th:replace="admin/_fragments::scripts"> /*/-->
<script src="https://cdn.bootcss.com/jquery/3.4.1/jquery.min.js"></script>
<script src="https://cdn.bootcss.com/semantic-ui/2.4.1/semantic.min.js"></script>
<!--/*/ </th:block> /*/-->

<script>
    $("header > a.toggle.button").click(function () {
        $(".m-header-item").toggleClass('m-hide-on-mobile');
        return false
    });
    $(".ui.dropdown").dropdown({
        on: 'hover'
    });

    function page(obj) {
        const pageNumber = $(obj).data('page');
        console.log(pageNumber);
        $("input[name=page]").val(pageNumber);
        loadArticles();
        return false;
    }

    function search() {
        $("input[name=page]").val(0);
        loadArticles();
    }

    function loadArticles() {
        const opts = {
            title: $("input[name='title']").val(),
            categoryId: $("input[name='categoryId']").val(),
            recommend: $("input[name='recommend']").is(":checked") ? true : undefined,
            page: $("input[name='page']").val()
        };
        console.log(opts);
        $("#table-container").load(/*[[@{/admin/blogs/search}]]*/"/admin/blogs/search", opts);
    }
</script>
</body>
</html>